#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main() {
	int n, i, j, sum, s1, s2, s;
	int a[4];
	cin >> n;
	while (n--) {
		cin >> s;
		sum = 1;
		while (s != 6174) {
			for (i = 0; i < 4; i++) {
				a[i] = s % 10;
				s = s / 10;
			}
			sort(a, a + 4);
			s2 = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
			s1 = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
			s = s2 - s1;
			sum++;
		}
		cout << sum << endl;
	}
	return 0;
}
